﻿/**************************************************
Author:DingXS 
Date:19/12/5
Version:0.1
Function List:图片重写
****************************************************
History:
<author>   <time>     <version>   <desc>
DingXS    19/12/5       0.1       -----
****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class TestDottedLine : Graphic
{

   public   Vector2[] vec;
    public bool Refresh = false;
    public Sprite ShowSprite;
    public bool Switch = false;
    public Color newColor;
    public List<Vector3> pos;
    private void Update()
    {
        if (Switch)
        {
            SetVerticesDirty();//刷新
        }
    }
    private void LateUpdate()
    {
        if (vec == null)
        {
            vec = new Vector2[pos.Count];
        }
       // List<Vector2> allPos = new List<Vector2>();
   
        for (int i = 0; i < pos.Count; i++)
        {
           
                Vector3 viewport = Camera.main.WorldToScreenPoint(pos[i]);
            Debug.Log(viewport.x+"-"+ viewport.y);
            vec[i] = new Vector2(viewport.x, viewport.y);
           
        }

       // vec = allPos.ToArray();
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
  
        vh.Clear();
        if (vec.Length > 0)
        {
            for (int i = 0; i < vec.Length; i = i + 4)
            {
                vh.AddVert(vec[i], newColor, new Vector2(0, 0));
                vh.AddVert(vec[i + 1], newColor, new Vector2(1, 0));
                vh.AddVert(vec[i + 2], newColor, new Vector2(1, 1));
                vh.AddVert(vec[i + 3], newColor, new Vector2(0, 1));
            }
            for (int j = 0; j < vec.Length ; j = j + 4)
            {
                vh.AddTriangle(j, j + 2, j + 1);
                vh.AddTriangle(j , j + 3, j + 2);
            }
        }
    }
    public override Texture mainTexture
    {
        get
        {
            return null;
        }
    }
    /// <summary>
    /// 判断点是否在窗口内
    /// </summary>
    /// <param name="vector"></param>
    /// <returns></returns>
    bool judgePostion(Vector3 vector)
    {
        Vector3 Viewport = Camera.main.WorldToViewportPoint(vector);
        if (Viewport.x > 0 && Viewport.x < 1 && Viewport.y > 0 && Viewport.y < 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}


